package com.ecarx.module_log.logexport.adapter;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.ecarx.library_base.usb.UDiskDevice;
import com.ecarx.module_log.R;

import java.util.ArrayList;
import java.util.List;

public class UsbDeviceAdapter extends BaseAdapter {

    private List<UDiskDevice> uDiskDeviceList;

    public UsbDeviceAdapter(List<UDiskDevice> uDiskDeviceList) {
        this.uDiskDeviceList = uDiskDeviceList;
        if (uDiskDeviceList.isEmpty()) {
            addPlaceData();
        }
    }

    public void notifyData(List<UDiskDevice> uDiskDeviceList) {
        this.uDiskDeviceList = uDiskDeviceList;
        if (uDiskDeviceList.isEmpty()) {
            addPlaceData();
        }
        notifyDataSetChanged();
    }

    private void addPlaceData() {
        uDiskDeviceList = new ArrayList<>();
        uDiskDeviceList.add(new UDiskDevice("U盘未连接"));
    }

    @Override
    public int getCount() {
        return uDiskDeviceList.size();
    }

    @Override
    public Object getItem(int position) {
        return uDiskDeviceList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_main_spinner, parent, false);
        TextView textView = (TextView) view.findViewById(R.id.tv_name);
        String fsLabel = uDiskDeviceList.get(position).getFsLabel();
        if (!TextUtils.isEmpty(fsLabel)) {
            textView.setText(fsLabel);
        } else {
            textView.setText(uDiskDeviceList.get(position).getUuid());
        }
        return view;
    }
}
